//
//  XHYWebViewController.m
//  XHYScrollingNavBarViewController
//
//  Created by smm_imac on 14-3-7.
//  Copyright (c) 2014年 XHY. All rights reserved.
//

#import "XHYWebViewController.h"
#define Url @"http://m.baidu.com/img?tn=bdidxiphone&itj=41&ssid=0&from=1000376c&bd_page_type=1&uid=0&pu=sz%401320_2001%2Cta%40iphone_1_7.0_3_537#"


@interface XHYWebViewController ()

@property (retain , nonatomic)UIWebView *webView;

@end

@implementation XHYWebViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)initView
{
    self.title = @"WebView";

    CGRect frame = CGRectMake(0, 0, 320, self.view.frame.size.height);
    self.webView = [[UIWebView alloc] initWithFrame:frame];
    self.webView.delegate=self;
    NSURL *url=[NSURL URLWithString:@"http://m.aojiyingyu.com/"];
    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:self.webView];
    
    self.navigationController.navigationBar.barTintColor=[UIColor greenColor];
    [self followRollingScrollView:self.webView];
    NSArray *subViews = self.webView.subviews;
    for (UIView* subView in subViews){
        if ([subView isKindOfClass:[UIScrollView class]]) {
            UIScrollView *scrollView = (UIScrollView *)subView;
            scrollView.alwaysBounceVertical = NO;
            scrollView.bounces = NO;
            for (UIView* shadowView in [subView subviews]){
                if ([shadowView isKindOfClass:[UIImageView class]]) {
                    [shadowView setHidden:YES];
                }
            }
        }
    }
    self.webView.opaque = NO;
    self.webView.backgroundColor = [UIColor whiteColor];
    navaView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 138/2)];
    [navaView setBackgroundColor:[UIColor colorWithRed:248.0f/255.0f green:248.0f/255.0f blue:248.0f/255.0f alpha:1.0f]];
    [self.view addSubview:navaView];
    [self performSelector:@selector(disappearTheNava) withObject:nil afterDelay:6];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame=CGRectMake(0, 20, 44, 44);
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backButton) forControlEvents:UIControlEventTouchUpInside];
    [navaView addSubview:backBtn];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.frame=CGRectMake(self.view.frame.size.width/2-50, 30, 100, 30);
    titleLabel.textColor = [UIColor colorWithRed:100.0f/255.0f green:100.0f/255.0f blue:100.0f/255.0f alpha:1.0f];
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.text=@"英语培训";
    titleLabel.textAlignment=NSTextAlignmentCenter;
    [navaView addSubview:titleLabel];
    
    self.panGesture = [[UIPanGestureRecognizer alloc] init];
    self.panGesture.delegate=self;
    self.panGesture.minimumNumberOfTouches = 1;
    [self.panGesture addTarget:self action:@selector(handlePanGesture:)];
    [self.view addGestureRecognizer:self.panGesture];

    
}

#pragma mark - 兼容其他手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

#pragma mark - 手势调用函数
-(void)handlePanGesture:(UIPanGestureRecognizer *)panGesture
{
    CGPoint translation = [panGesture translationInView:[self.view superview]];
    
    NSLog(@"x:%f",translation.x);
    NSLog(@"y:%f",translation.y);
    if (translation.y >= 10) {
        [self.view bringSubviewToFront:navaView];
        [self performSelector:@selector(disappearTheNava) withObject:nil afterDelay:3];
    }
    if(translation.x >185){
        [self.navigationController popViewControllerAnimated:YES];
    }
}


//返回设置
-(void)backButton
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)disappearTheNava{
    [self.view sendSubviewToBack:navaView];
}

-(void)nextVc:(UIBarButtonItem *)item
{
}

-(void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"%@",webView.request.URL);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    [self initView];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
}

@end
